package com.wk1un2.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wk1un2.entity.ChooseModifyCounselor;
import com.wk1un2.entity.Exportexcel;
import com.wk1un2.entity.Sign;
import com.wk1un2.entity.Students;
import com.wk1un2.service.SignService;
import com.wk1un2.service.StudentsService;
import com.wk1un2.service.SwitchService;
import com.wk1un2.util.Result;

@RequestMapping("/public")
@RestController
public class StudentsController {
	@Autowired
	private StudentsService studentsservice;
	@Autowired
	SwitchService switchService;
	@Autowired
	SignService signService;

	List list = new ArrayList<>();

	@RequestMapping("/getAllStudents")
	public Object getAll(@RequestParam Map<String, Object> map, Integer page, Integer limit) {

		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		List<Map<String, Object>> ls = studentsservice.getAll(map);
		System.out.println("==========>" + map);
		Result res = new Result(0, "success", studentsservice.count(), ls);
		return res;
	}

	@RequestMapping("getDispense")
	public Object getDispense(@RequestParam Map<String, Object> map, Integer page, Integer limit) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		Result res = new Result(0, "success", studentsservice.countDispense(map), studentsservice.getDispense(map));
		return res;
	}

	@RequestMapping("getUndistributed")
	public Object getUndistributed(@RequestParam Map<String, Object> map, Integer page, Integer limit) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		Result res = new Result(0, "success", studentsservice.countUndistributed(map), studentsservice.getUndistributed(map));
		return res;
	}

	@RequestMapping("/addStudents")
	public Result insert(Students students) {
		students.setDepositTime(new Date());
		System.out.println("开关状态：" + switchService.getState().get(0).getState());
		if (switchService.getState().get(0).getState().equals(1)) {
			if (list == null || list.size() == 0) {
				List<Sign> consultant = signService.getSignConsultant(1, 1000, null, null);
				for (int i = 0; i < consultant.size(); i++) {
					list.add(consultant.get(i).getUserid());
				}
				System.out.println("list===========>>>>>>>>" + list);
			}
			System.out.println("list.get(0)========>>>>>>>>" + list.get(0));
			students.setUserid(Integer.parseInt(list.get(0).toString()));
			list.remove(0);
			System.out.println("removelist=========>>>>>>>>" + list);
		}
		Result res = new Result(0, "添加成功!!!", studentsservice.insert(students), true);
		return res;

	}

	@RequestMapping("excel")
	public Object excel(Exportexcel excel) {
		Result res = null;
		if (excel.getType().equals(1)) {
			if (excel.getTwo() != null) {
				for (int i = 0; i < excel.getTwo().size(); i++) {
					excel.getOne().remove(excel.getTwo().get(i));
				}
			}
			res = new Result(0, "success", 10, studentsservice.getChecked(excel.getOne()));
		} else if (excel.getType().equals(2)) {

		}
		return res;
	}

	@RequestMapping("/updateStudents")
	public Result updateById(Students students) {
		Result res = new Result(0, "修改成功", studentsservice.update(students), true);
		return res;
	}

	@RequestMapping("/deleteStudents")
	public Result deleteById(Integer stuid) {
		Map<String, Object> map=new HashMap<>();
		map.put("stuid", stuid);
		Result res = null;
		if (studentsservice.getDispense(map).size()>0) {
			res=new Result(0, "删除失败，该学生已分配咨询师", -1, false);
		} else {
			res=new Result(0, "删除成功", studentsservice.delete(stuid), stuid);
		}
		return res;
	}

	@RequestMapping("/deleteStudents2")
	public Result deleteById2(Integer stuid) {
		Result res = new Result(0, "删除成功", studentsservice.delete(stuid), true);
		return res;
	}
	// 查询我（咨询师下）的学生
	@RequestMapping("/getByUserid")
	public Object getByUserid(@RequestParam Map<String, Object> map, Integer page, Integer limit) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		List<Students> ls = studentsservice.getByUserid(map);
		System.out.println("==========>" + map);
		System.out.println();
		Result res = new Result(0, "success", studentsservice.count(map), ls);
		return res;
	}

	// 查询网络学生
	@RequestMapping("/getByLineUserid")
	public Object getByLineUserid(@RequestParam Map<String, Object> map, Integer page, Integer limit) {
		map.put("page", (page - 1) * limit);
		map.put("limit", limit);
		List<Students> ls = studentsservice.getByLineUserid(map);
		Result res = new Result(0, "success",studentsservice.getByLineUseridCount(map), ls);
		return res;
	}

	// 批量修改咨询师
	@RequestMapping("getChooseModifyCounselor")
	public Object getChooseModifyCounselor(ChooseModifyCounselor cmc) {
		if (cmc.getTwo() != null) {
			for (int i = 0; i < cmc.getTwo().size(); i++) {
				cmc.getOne().remove(cmc.getTwo().get(i));
			}
		}
		Result res = new Result(0, "success", studentsservice.getChooseModifyCounselor(cmc.getOne(), cmc.getUserid()),
				true);
		return res;
	}

}
